<template>
  <el-dialog title="新增角色" :visible="isShow" @close="closeEvent">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRolePost, sysRolePut } from '@/api/setting'
export default {
  model: {
    prop: 'isShow',
    event: 'input'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        name: '', //	string	必须		角色名称
        description: '' //	string	必须		角色描述
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        description: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      mode: 'add'
    }
  },
  methods: {
    // 关闭弹窗
    closeEvent() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 提交表单
    submit() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          if (this.mode === 'add') {
            await sysRolePost(this.form)
            this.$message.success('新增成功')
          } else {
            await sysRolePut(this.form)
            this.$message.success('编辑成功')
          }
          this.closeEvent()
          this.$parent.getData() // this.$parent 获取的是最近一级的父组件,需要注意
        }
      })
    }
  }
}
</script>

<style></style>
